{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Streaming\n",
    "\n",
    "All ChatModels implement the Runnable interface, which comes with default implementations of all methods, ie. ainvoke, batch, abatch, stream, astream. This gives all ChatModels basic support for streaming.<br>\n",
    "所有 ChatModel 都实现了 Runnable 接口，该接口附带了所有方法的默认实现，即: ainvoke、batch、abatch、stream、astream。这为所有 ChatModel 提供了对流式处理的基本支持。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Verse 1:\n",
      "Swimming through the galaxy\n",
      "In a world so far away\n",
      "Goldfish on the moon\n",
      "Where they dance and they play\n",
      "\n",
      "Chorus:\n",
      "Goldfish on the moon\n",
      "Shining bright in the sky\n",
      "In a world of wonder\n",
      "Where they never say goodbye\n",
      "\n",
      "Verse 2:\n",
      "Their scales shimmering like stars\n",
      "As they glide through the silver sea\n",
      "Goldfish on the moon\n",
      "Living wild and free\n",
      "\n",
      "Chorus:\n",
      "Goldfish on the moon\n",
      "Swimming in zero gravity\n",
      "In a world of mystery\n",
      "Where they find serenity\n",
      "\n",
      "Bridge:\n",
      "In a place where dreams come true\n",
      "And the sky is never blue\n",
      "Goldfish on the moon\n",
      "Forever in bloom\n",
      "\n",
      "Chorus:\n",
      "Goldfish on the moon\n",
      "In a cosmic ballet\n",
      "In a world of enchantment\n",
      "Where they'll always stay\n",
      "\n",
      "Outro:\n",
      "So let's dream of the goldfish\n",
      "On the moon up above\n",
      "In a world of magic\n",
      "Filled with endless love."
     ]
    }
   ],
   "source": [
    "from langchain_openai import ChatOpenAI\n",
    "\n",
    "chat = ChatOpenAI()\n",
    "for chunk in chat.stream(\"Write me a song about goldfish on the moon\"):\n",
    "    print(chunk.content, end=\"\", flush=True)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "langchain0_1",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
